package com.echoesnet.echoesshortvideo.ViewModels;

import android.arch.lifecycle.LiveData;
import android.arch.lifecycle.MutableLiveData;
import android.arch.lifecycle.ViewModel;

import com.baidu.mapapi.search.core.PoiInfo;
import com.baidu.mapapi.search.poi.OnGetPoiSearchResultListener;
import com.baidu.mapapi.search.poi.PoiDetailResult;
import com.baidu.mapapi.search.poi.PoiIndoorResult;
import com.baidu.mapapi.search.poi.PoiResult;
import com.echoesnet.echoesshortvideo.Repositories.SearchLocationRepository;
import com.echoesnet.echoesshortvideo.Repositories.SelectLocationRepository;
import com.orhanobut.logger.Logger;

import java.util.ArrayList;
import java.util.List;

/**
 * Copyright (C) 2017 在线回声（天津）科技发展有限公司
 * 在线回声完全享有此软件的著作权，违者必究
 *
 * @author an
 * @version 1.0
 * @modifier
 * @createDate 2018/4/25 0025
 * @description
 */
public class SearchLocationModel extends ViewModel
{
    private final static String TAG = SearchLocationModel.class.getSimpleName();
    private MutableLiveData<List<PoiInfo>> data;
    private SearchLocationRepository searchLocationRepository;
    private OnGetPoiSearchResultListener onGetPoiSearchResultListener;

    public SearchLocationModel()
    {
        searchLocationRepository = new SearchLocationRepository();
        data = new MutableLiveData<>();
        List<PoiInfo> poiInfos = new ArrayList<>();
        data.setValue(poiInfos);
    }

    public MutableLiveData<List<PoiInfo>> getData()
    {
        return data;
    }

    public void searchLocation(String city, String keywords, int pageNum){
            if (onGetPoiSearchResultListener == null)
            {
                searchLocationRepository.init(new OnGetPoiSearchResultListener()
                {
                    @Override
                    public void onGetPoiResult(PoiResult poiResult)
                    {
                        if (poiResult.getAllPoi() != null)
                        Logger.t(TAG).d("搜索结果>>>>" + poiResult.getAllPoi().toString());
                        //获取POI检索结果
                        List<PoiInfo> allPoi = poiResult.getAllPoi();
                        List<PoiInfo> poiInfos = data.getValue();
                        if (pageNum == 0)
                        {
                            poiInfos.clear();
                        }
                        if (allPoi != null)
                        {
                            poiInfos.addAll(allPoi);
                        }
                        data.setValue(poiInfos);
                    }

                    @Override
                    public void onGetPoiDetailResult(PoiDetailResult poiDetailResult)
                    {

                    }

                    @Override
                    public void onGetPoiIndoorResult(PoiIndoorResult poiIndoorResult)
                    {

                    }

                });
            }
            searchLocationRepository.searchLocation(city,keywords,pageNum);
    }

    public void onDestroy()
    {
        if (searchLocationRepository != null)
            searchLocationRepository.onDestroy();
    }
}
